using System.Collections.Generic;
using System;
using SrBBSPro.BaseClasses;
using SrBBSPro.Entities;

namespace SrBBSPro.Util
{
    public class DataHelper
    {
        public static Dictionary<string, object> GetResult(bool success, string message, object data = null)
        {
            var d = new Dictionary<string, object>
            {
                { "success",success },
                { "message",message}
            };
            if (data != null)
                d.Add("data", data);

            return d;
        }

        /// <summary>
        /// 返回适合Fly模板的格式
        /// </summary>
        /// <param name="status">状态 0为正常</param>
        /// <param name="msg">提示信息</param>
        /// <returns></returns>
        public static Dictionary<string, object> GetResult(int status, string msg)
        {
            return new Dictionary<string, object>
            {
                { "status",status },
                { "msg",msg}
            };
        }
        /// <summary>
        /// 返回适合Fly模板的格式
        /// </summary>
        /// <param name="status">状态 0为正常</param>
        /// <param name="msg">提示信息</param>
        /// <param name="dict">其他数据的集合</param>
        /// <returns></returns>
        public static Dictionary<string, object> GetResult(int status, string msg,Dictionary<string,object> dict)
        {
            dict.Add("status", status);
            dict.Add("msg", msg);
            return dict;
        }
        /// <summary>
        /// 返回适合Fly模板的格式
        /// </summary>
        /// <param name="status">状态 0为正常</param>
        /// <param name="msg">提示信息</param>
        /// <param name="action">跳转地址</param>
        /// <returns></returns>
        public static Dictionary<string, object> GetResult(int status, string msg,string action)
        {
            return new Dictionary<string, object>
            {
                { "status",status },
                { "msg",msg},
                { "action",action}
            };
        }
        /// <summary>
        /// 返回适合Fly模板的格式
        /// </summary>
        /// <param name="status">状态 0为正常</param>
        /// <param name="msg">提示信息</param>
        /// <param name="code">数据</param>
        /// <returns></returns>
        public static Dictionary<string, object> GetResult(int status, string msg, object rows)
        {
            return new Dictionary<string, object>
            {
                { "status",status },
                { "msg",msg},
                { "rows",rows}
            };
        }
        /// <summary>
        /// 返回适合Fly模板的格式
        /// </summary>
        /// <param name="status">状态 0为正常</param>
        /// <param name="msg">提示信息</param>
        /// <param name="rows">数据</param>
        /// <param name="code">代码</param>
        /// <returns></returns>
        public static Dictionary<string, object> GetResult(int status, string msg, object rows, string code)
        {
            return new Dictionary<string, object>
            {
                { "status",status },
                { "msg",msg},
                { "rows",rows},
                { "code",code}
            };
        }

        public static Dictionary<string, object> GetResult(Dictionary<string, string> dic)
        {
            var list = new List<Dictionary<string, string>>();
            foreach (var item in dic)
            {
                list.Add(new Dictionary<string, string>()
                    {
                         { "val",item.Key},
                         { "text",item.Value}
                    });
            }
            return new Dictionary<string, object>
            {
               { "status",0 },
               { "msg",list}
            };
        }

        public static Dictionary<string, object> GetResult<T,TID>(List<T> li ,int count, bool IsSkill) where T : BaseEntity<TID>
        {
            var list = new List<Dictionary<string, object>>();
            foreach (var obj in li)
            {
                var dic = new Dictionary<string, object>();
                foreach (var prop in obj.GetType().GetProperties())
                {
                    var val = prop.GetValue(obj);
                    if (val != null && val.GetType() == typeof(DateTime))
                    {
                        dic.Add(prop.Name, Convert.ToDateTime(val).ToString("yyyy/MM/dd"));
                    }
                    else if (val != null && val.GetType() == typeof(User))
                    {
                        var user = (User)val;
                        dic.Add("StaffNo", user.Id);
                        dic.Add("StaffName", user.StaffName);
                        dic.Add("DepartName", user.DepartName);
                        dic.Add("Depart", user.Depart);
                    }                 
                    else if (IsSkill && val != null && val.GetType() == typeof(System.String))
                    {
                        dic.Add(prop.Name, SkillLevel[Convert.ToString(val)]);
                    }
                    else
                    {
                        dic.Add(prop.Name, val);
                    }
                }
                list.Add(dic);
            }            

            return new Dictionary<string, object>
            {
                { "code",0},
                { "msg",""},
                { "count",count==0?li.Count:count},
                { "data",list}
            };
        }

        public static Dictionary<string, object> GetResult<T>(List<T> li) 
        {
            var list = new List<Dictionary<string, object>>();
            foreach (var obj in li)
            {
                var dic = new Dictionary<string, object>();
                foreach (var prop in obj.GetType().GetProperties())
                {
                    var val = prop.GetValue(obj);                
                    dic.Add(prop.Name, val);                    
                }
                list.Add(dic);
            }

            return new Dictionary<string, object>
            {
                { "code",0},
                { "msg",""},
                { "count",li.Count},
                { "data",list}
            };
        }


        #region 数据字典 
        public static Dictionary<string, string> SkillLevel = new Dictionary<string, string>()
        {
            {"0","不具备"},
            {"1","一般"},
            {"2","熟练"},
            {"3","精通"}
        };


        public static Dictionary<string, string> DepartName = new Dictionary<string, string>()
        {
            {"1","开发一部"},
            {"2","开发二部"},
            {"3","开发三部"},
            {"4","开发四部"},
            {"5","开发五部"},
            {"6","开发六部"}
        };      
        #endregion
    }
}
